package exercises.newcoder.solution;

import java.util.Scanner;

/**
 * <a href="https://www.nowcoder.com/study/live/718/1/5">
 * 贪心-数轴覆盖(牛客网-算法中级班)</a>
 *
 * @author or2
 * @Description 数轴上有N个点，求一条长度为K的线段最多覆盖多少个点？
 * @create 2021年09月15日 时间: 21:38
 */
public class CoverageArea {
    public static void main(String[] args) {
//        答案变量
        int ans = 0;
//        处理输入
        Scanner scanner = new Scanner(System.in);
        int totalNums = scanner.nextInt();
        int area = scanner.nextInt();
        int[] inInts = new int[totalNums];
        for (int i = 0; i < totalNums; i++) {
            inInts[i] = scanner.nextInt();
        }

        int leftIndex = 0;
        int rightIndex = leftIndex;
        while (rightIndex < totalNums) {
//            确定左边范围覆盖的点 - (]
            while (inInts[leftIndex] + area - 1 < inInts[rightIndex]) {
                leftIndex++;
            }
//            计算点个数
            int div = rightIndex - leftIndex + 1;
            if (div > ans) {
                ans = div;
            }
//            找下一个点
            rightIndex++;
        }
    }
}
